// ==UserScript==
// @name         一键批量下载1688中文站图片/视频
// @namespace    https://detail.1688.com
// @version	     1.0.0
// @description	 一键批量下载1688中文站主图，详情页
// @author       LaoTie
// @homepage
// @match        https://detail.1688.com/*
// @grant        GM_log
// @grant        GM_setClipboard
// @grant        GM_notification
// @grant        GM_download
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant	     GM_addStyle
// ==/UserScript==
/*jshint multistr:true */


window.onload = async function () {
    //
    await addButton();
    // galleryImgs();
}

function addButton() {
    const downBox = document.createElement('button');
    downBox.innerHTML = '下载轮播图和视频';

    const infoBox = document.createElement('button');
    infoBox.innerHTML = '获取商品信息';
    const buttonBox = document.querySelector('.layout-right div');
    buttonBox.appendChild(downBox);
    buttonBox.appendChild(infoBox);

    //add onclick
    downBox.onclick = () => galleryImgs();
    infoBox.onclick = () => getInfo();


}

function getInfo() {
    let custom_info = {};
    custom_info.name = document.querySelector('.title-content div .title-text')?.innerText;
    const piprice = document.querySelectorAll('.price-column div span');
    if (piprice.length > 0) {
        let price = {};
        price.priceStart = piprice[1].innerText;
        price.priceEnd = piprice[3].innerText;
        price.priceUnit = piprice[0].innerText;
        custom_info.price = price;
    }
    const daifa_price = document.querySelector('.price-content .bottom .price')?.innerText;
    custom_info.daifa_price = daifa_price;
    const piCount = document.querySelector('.unit-box span')?.innerText;
    const colorList = document.querySelectorAll('.prop-item-wrapper div div .prop-name');
    const colorBackList = document.querySelectorAll('.prop-item-wrapper div div .prop-img');
    const color = [];
    for (let i = 0; i < colorList.length; i++) {
        const colorName = colorList[i].innerText;
        const colorBack = colorBackList[i].getAttribute('style').match('(?<=\(\")(.+?)(?=\"\))')[2];
        color.push({ colorName, colorBack });
    }
    custom_info.color = color;
    const guigeNameList = document.querySelectorAll('.sku-item-wrapper div .sku-item-name');
    const guigePriceList = document.querySelectorAll('.sku-item-wrapper div .discountPrice-price');
    const guigeSaleList = document.querySelectorAll('.sku-item-wrapper div .sku-item-sale-num');
    const guiges = [];
    for (let i = 0; i < guigeNameList.length; i++) {
        const guigeName = guigeNameList[i].innerText;
        const guigePrice = guigePriceList[i].innerText;
        const guigeSale = guigeSaleList[i].innerText;
        guiges.push({ guigeName, guigePrice, guigeSale });
    }
    custom_info.guiges = guiges;
    // console.log(name, priceStart, priceEnd, priceUnit, piCount, color);
    // console.log(guiges);
    saveDataToFile(custom_info,'custom_info.json');
}

function galleryImgs() {
    const video = document.querySelector('.detail-video-wrapper div video').src;
    console.log(video);
    GM_download(video, 'video.mp4');
    //
    const galleryImgs = document.querySelectorAll('.img-list-wrapper div .detail-gallery-img');

    for (let i = 0; i < galleryImgs.length; i++) {
        const img = galleryImgs[i].src;
        GM_download(img, 'img' + i + '.jpg');
    }
}

function saveDataToFile(data, fname) {
    var blob = new Blob([JSON.stringify(data)], { type: 'data:application/json' });
    var tempLink = document.createElement('a');
    tempLink.href = URL.createObjectURL(blob);
    tempLink.download = fname;
    tempLink.click();
    URL.revokeObjectURL(tempLink.href);
}